package com.org.manhu.chargehelper.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.org.manhu.chargehelper.R;
import com.org.manhu.chargehelper.tools.LoadImage;
import com.org.manhu.chargehelper.ui.NetDetialActivity;
import com.org.manhu.chargehelper.ui.VideoDetialActivity;

/**
 *
 * @文件名：ExchangeAdapter.java
 * @包名：com.org.manhu.Adapter
 * @作者：李朋辉 @ QQ：892839211
 * @创建时间：2014年11月27日 下午6:01:20
 * @描述：用来加载服务器返回的数据的适配器
 */
public class NetAppListAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private List<Map<String, String>> mList;
	private Context context;

	public NetAppListAdapter(Context context, List<Map<String, String>> list) {
		this.context = context;
		inflater = LayoutInflater.from(context);
		mList = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
		// return 10;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
		// return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_app_list, null);
			holder.appIcon = (ImageView) convertView
					.findViewById(R.id.app_icon);
			holder.appNameTv = (TextView) convertView
					.findViewById(R.id.app_name);
			holder.costTv = (TextView) convertView
					.findViewById(R.id.cost_grade);
			holder.summaryTv = (TextView) convertView
					.findViewById(R.id.app_describe);
			holder.exchangeBtn = (Button) convertView
					.findViewById(R.id.down_btn);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final Map<String, String> map = mList.get(position);
		LoadImage.LoadPic(map.get("IcoUrl"), holder.appIcon);
		holder.appNameTv.setText(map.get("AppName"));
		String userPrice = map.get("UserPrice");
		if ("0".equals(userPrice)) {
			holder.costTv.setVisibility(View.GONE);
		}
		holder.costTv.setText("安装奖励：" + userPrice + "元");
		holder.summaryTv.setText(map.get("Digest"));
		holder.exchangeBtn.setVisibility(View.GONE);

		if ("3".equals(map.get("TypeID"))) {
			holder.costTv.setVisibility(View.GONE);
		}

		holder.exchangeBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

			}
		});

		holder.appIcon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				if ("3".equals(map.get("TypeID"))) {
					intent.setClass(context, VideoDetialActivity.class);
				} else {
					intent.setClass(context, NetDetialActivity.class);
				}
				intent.putExtra("appid", map.get("AppID"));
				context.startActivity(intent);
			}
		});

		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				if ("3".equals(map.get("TypeID"))) {
					intent.setClass(context, VideoDetialActivity.class);
				} else {
					intent.setClass(context, NetDetialActivity.class);
				}
				intent.putExtra("appid", map.get("AppID"));
				context.startActivity(intent);
			}
		});
		return convertView;
	}

	public void setData(List<Map<String, String>> data) {
		mList.addAll(data);
	}

	public void clearData() {
		mList.clear();
	}

	class ViewHolder {
		ImageView appIcon;
		TextView appNameTv, costTv, summaryTv;
		Button exchangeBtn;
	}

}
